package com.google.shop.product.controller.member;

import com.google.shop.product.common.utils.JwtUtils;
import com.google.shop.product.common.utils.R;
import com.google.shop.product.repository.member.BuyerInfoRepository;
import com.google.shop.product.service.member.BuyerInfoService;
import com.google.shop.product.vo.member.BuyerInfoVO;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/member"})
@RestController
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/controller/member/BuyerInfoController.class */
public class BuyerInfoController {
    private static final Logger log = LoggerFactory.getLogger(BuyerInfoController.class);
    @Autowired
    BuyerInfoService buyerInfoService;
    @Autowired
    BuyerInfoRepository buyerInfoRepository;

    @PostMapping({"/signIn"})
    public R signIn(@Valid @RequestBody BuyerInfoVO buyerInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【注册邮箱】参数错误,addressForm={}", buyerInfoVO);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            this.buyerInfoService.saveBuyerInfo(buyerInfoVO);
            return R.ok();
        } catch (Exception e) {
            return R.error("邮箱已存在");
        }
    }

    @PostMapping({"/login"})
    public R login(@Valid @RequestBody BuyerInfoVO buyerInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【注册邮箱】参数错误,addressForm={}", buyerInfoVO);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            return this.buyerInfoService.login(buyerInfoVO);
        } catch (Exception e) {
            return R.error("登录系统异常");
        }
    }

    @GetMapping({"getMemberInfo"})
    public R getMemberInfo(HttpServletRequest request) {
        String shoptoken = request.getHeader("shoptoken");
        if (shoptoken == null) {
            return R.error("token为空");
        }
        return R.ok(this.buyerInfoRepository.findByBuyerId(Integer.valueOf(JwtUtils.getMemberIdByJwtToken(shoptoken))));
    }
}
